package com.training.hatton.base.processor;

import org.apache.http.client.methods.HttpRequestBase;

import android.os.Bundle;
import android.os.ResultReceiver;

import com.training.hatton.base.RequestDataWrapper;
import com.training.hatton.dropboxclient.utill.DropboxService;

public abstract class BaseProcessor<T> implements IProcessor<T> {
	
	public static final int IN_PROGRESS_CODE = 0;
	public static final int SUCCESS_CODE = 1;
	public static final int ERROR_CODE = 2;
	public static final int START_CODE = 3;
	
	protected ResultReceiver mResultReceiver;
	protected RequestDataWrapper mRequestData;
	
	public BaseProcessor(RequestDataWrapper dataWrapper, ResultReceiver resultReceiver){
		mResultReceiver = resultReceiver;
		mRequestData = dataWrapper;
	}
	
	public void start(){
		try{
			sendData(START_CODE, null);
			HttpRequestBase request = buildHttpRequest(mRequestData.getUrl());
			executeProcess(request);
		}catch(Exception e){
			Bundle resultData = new Bundle();
			resultData.putSerializable(DropboxService.EXTRA_ERROR, e);
			sendData(ERROR_CODE, resultData);
		}
	}
	
	public ResultReceiver getResultReceiver() {
		return mResultReceiver;
	}

	public void setResultReceiver(ResultReceiver resultReceiver) {
		this.mResultReceiver = resultReceiver;
	}

	public RequestDataWrapper getRequestData() {
		return mRequestData;
	}

	public void setRequestData(RequestDataWrapper requestData) {
		this.mRequestData = requestData;
	}

	public void sendData(int resultCode, Bundle resultData){
		getResultReceiver().send(resultCode, resultData);
	}
	
	public abstract HttpRequestBase buildHttpRequest(String url);
}
